package com.doxiaopu.students.user.controller;

import com.doxiaopu.students.component.controller.BaseController;
import com.doxiaopu.students.component.result.ResponseResult;
import com.doxiaopu.students.user.service.IMerchantService;
import com.doxiaopu.students.user.vo.MerchantVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Api(tags = "商家信息信息")
@RestController
@CrossOrigin
@RequestMapping("/merchant")
public class MerchantController extends BaseController {
    @Autowired
    private IMerchantService iMerchantService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @ApiOperation(value = "创建或修改商家信息")
    @PostMapping("/createOrUpdateMerchantInfo")
    public ResponseResult<Void> CreateMerchant(HttpServletRequest request,MerchantVO merchantVO) {
        Integer id =getUidFromRequest(request);
        if(id !=null) {
            System.err.print(merchantVO);
            if(merchantVO.gettUserId() ==null || merchantVO.gettUserId() =="") {
                Integer row = iMerchantService.CreateMerchant(merchantVO,id);
                System.err.print("新建："+row);
            }else {
                Integer row = iMerchantService.updateMerchant(merchantVO,id);
                System.err.print("编辑："+row);
            }
            return new ResponseResult<Void>(SUCCESS);
        }else {
            return new ResponseResult<Void>(408,"登录过期请重新登录");
        }
    }

    @ApiOperation(value = "获得商家列表信息")
    @PostMapping("/getMerchantList")
    public ResponseResult<Map<String, Object>> getMerchantList(String phone, String storeName, Integer currentPage, Integer pageSize){
        Map<String, Object> data = iMerchantService.findMerchantList( phone, storeName, currentPage, pageSize);
        return new ResponseResult<Map<String, Object>>(SUCCESS,data);
    }
    private Integer getUidFromRequest(HttpServletRequest request){
        //获取headers中的参数
        String token = request.getHeader("token");
        if(stringRedisTemplate.opsForValue().get(token) != null) {
            return Integer.valueOf(stringRedisTemplate.opsForValue().get(token).toString());
        }else {
            return null;
        }
    }
}
